<template>
  账号密码登录
  <el-form class="form-show" :rules="loginRules" :model="form">
    <el-form-item prop="username">
      <el-input
        v-model="form.username"
        placeholder="请输入用户名"
        :prefix-icon="User"
        clearable
      ></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        v-model="form.password"
        type="password"
        placeholder="请输入密码"
        :prefix-icon="Lock"
        show-password
        @keyup.enter="login"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button class="login-button" type="primary" @click="login"
        >登录</el-button
      >
    </el-form-item>
  </el-form>
  <span id="toRegister" @click="toRegister">注册账户</span>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue"
import { useRouter } from "vue-router"
import { User, Lock } from "@element-plus/icons-vue"
import { UserService } from "@/script/axios/api/api"

const form = reactive({
  username: "",
  password: "",
})

const loginRules = reactive({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 12, message: "密码长度在6-12位", trigger: "blur" },
  ],
})

const login = async () => {
  console.log("click login")

  const res: any = await UserService.login({
    username: form.username,
    password: form.password,
  })

  if (res.code === 200) {
    sessionStorage.setItem("userInfo", res.data)
    router.push("/home")
  }
}

const router = useRouter()
const toRegister = () => {
  router.push("/register")
}
</script>

<style lang="scss" scoped>
.form-show {
  width: calc(100% - 10px);
  .el-form-item {
    margin-bottom: 20px;
  }
}
</style>
